package com.wowangz.cms.common.api;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.wowangz.cms.component.datagrid.pojo.DataGrid;
import com.wowangz.cms.component.datagrid.pojo.DataResult;
import com.wowangz.cms.component.datagrid.pojo.DataStyle;
import com.wowangz.cms.component.datagrid.pojo.PangingParam;
import com.wowangz.cms.component.datagrid.utils.DataTypeConstant;
import com.wowangz.cms.component.datagrid.utils.PangingTools;
import com.wowangz.cms.syscore.InitEnvironment;
import com.wowangz.cms.syscore.api.BaseController;
import com.wowangz.cms.syscore.api.JSONFactory;
import com.wowangz.cms.syscore.utils.LoadBaseDataConstants;
import com.wowangz.cms.syscore.utils.ServletUtils;
import com.wowangz.cms.systemmanage.headbank.pojo.HeadBank;

@Scope("prototype")
@Controller
public class CacheDataController extends BaseController {
	
	private static Logger logger = LoggerFactory.getLogger(CacheDataController.class);
	
	@RequestMapping(value = "/common/queryHeadBankInfoDataGrid.json", method = RequestMethod.POST)
	public void queryHeadBankInfoDataGrid(HttpServletRequest request, HttpServletResponse response, Model model) {
		
		PangingParam pangingParam = null;
		DataResult dataResult = new DataResult();
		DataGrid dataGrid = null;
		
		try {
			pangingParam = (PangingParam) ServletUtils.getParametersToBaseEntity(request, PangingParam.class);
			HashMap<String, Object> map = (HashMap) ServletUtils.getParametersToHashMap(request);
			dataGrid = new DataGrid();
			dataGrid.setPangingParam(pangingParam);
			List list = InitEnvironment.getBaseDataCacheMap().get(LoadBaseDataConstants.BASE_DATA_HEADBANK);
			List tmpList = new ArrayList();
			int start = (int)dataGrid.getStartrow();
			int end = (int)dataGrid.getEndrow();
			if(StringUtils.isNotEmpty((String) map.get("bankname"))){
				for (int i = 0; i < list.size(); i++) {
					HeadBank bank = (HeadBank)list.get(i);
					if(StringUtils.contains(bank.getBankname(), (String)map.get("bankname"))){
						tmpList.add(list.get(i));
					}
				}
			} else {
				tmpList = list;
			}
			List resultList = new ArrayList();
			if(end <= tmpList.size()){
				resultList = tmpList.subList(start, end);
			}else{
				resultList = tmpList.subList(start, tmpList.size());
			}
			DataStyle dataStyle = new DataStyle();
			dataStyle.setPageIndex(pangingParam.getPage());
			dataStyle.setDataCount(tmpList.size());
			dataStyle.setDataList(resultList);
			
			dataStyle.addDepict("id", DataTypeConstant.STRING);
			dataStyle.addDepict("banknum", DataTypeConstant.STRING);
			dataStyle.addDepict("bankname", DataTypeConstant.STRING);
			dataStyle.addDepict("directcode", DataTypeConstant.STRING);
			dataResult = PangingTools.buildResultPagerInfo(dataStyle);
			if (pangingParam.getReportExportType() == 0) {
				response.getWriter().write(JSONFactory.toJSONString(dataResult, userContext));
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
